package com.dhcc.cdc.busi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcDictionaryMapper;
import com.dhcc.cdc.busi.dao.CdcDictypeMapper;
import com.dhcc.cdc.busi.dao.CdcLabItemMapper;
import com.dhcc.cdc.busi.dao.CdcUserMapper;
import com.dhcc.cdc.busi.entity.CdcDictionaryEntity;
import com.dhcc.cdc.busi.entity.CdcDictypeEntity;
import com.dhcc.cdc.busi.entity.CdcLabItemEntity;
import com.dhcc.cdc.busi.entity.CdcUserEntity;
import com.dhcc.cdc.busi.service.ICdcLabItemService;
import com.dhcc.cdc.sys.dao.CdcDataMapper;
import com.dhcc.cdc.sys.entity.CdcDataEntity;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 检验项目表数据服务层接口
 * 
 * @ClassName: LabItemServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-04-26 16:22:25
 */
@Service("labItemService")
public class CdcLabItemServiceImpl extends BaseServiceImpl<CdcLabItemMapper, CdcLabItemEntity> implements ICdcLabItemService {
	@Autowired
    private CdcDictionaryMapper cdcDictionaryMapper;
	
	@Autowired
	private CdcDictypeMapper cdcDictypeMapper;
	
	@Autowired
	private CdcUserMapper cdcUserMapper;
	
	@Autowired
	private CdcDataMapper cdcDataMapper;
	@Override
	public List<CdcLabItemEntity> page(Page<CdcLabItemEntity> page, Map<String, Object> map) {
		return this.baseMapper.findPage(page, map);
	}
	@Override
	public List<CdcLabItemEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}
	@Override
	public String handleXmlData(String dataStr) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Document doc = null;
        try {
            doc = DocumentHelper.parseText(dataStr); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            Iterator<Element> iter = rootElt.elementIterator("labitem"); // 获取根节点下的子节点labitem
            // 遍历labitem节点
            while (iter.hasNext()) {
                Element labitemEle = iter.next();
                String xcode= labitemEle.elementTextTrim("xcode"); //唯一标识
                String code= labitemEle.elementTextTrim("code"); //代码	
                String description= labitemEle.elementTextTrim("description"); //描述
                String significance= labitemEle.elementTextTrim("significance"); //临床意义
                String normalvalue= labitemEle.elementTextTrim("normalvalue"); //正常参考值
                String buslabvaluetypecode= labitemEle.elementTextTrim("buslabvaluetypecode"); //结果类型	
                String buslabvaluetypedesc= labitemEle.elementTextTrim("buslabvaluetypedesc"); //结果类型
                String abbreviation= labitemEle.elementTextTrim("abbreviation"); //缩写码	
                String note= labitemEle.elementTextTrim("note"); //备注
                String updatetimestr= labitemEle.elementTextTrim("updatetime"); //更新时间
                String updateuserid= labitemEle.elementTextTrim("updateuserid"); //更新人
                String opercode= labitemEle.elementTextTrim("opercode"); //操作码
                CdcLabItemEntity labitem = new CdcLabItemEntity();
                labitem.setXcode(xcode);
                //获取dataid
    			CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
    			cdcDataEntityBase.setCode("HisBase");
    			cdcDataEntityBase.setIsActive(1);
    			cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
        		Long basedataId = cdcDataEntityBase.getId();
        		//获取dataid
    			CdcDataEntity cdcDataEntityLab = new CdcDataEntity();
    			cdcDataEntityLab.setCode("LabData");
    			cdcDataEntityLab.setIsActive(1);
    			cdcDataEntityLab = cdcDataMapper.selectOne(cdcDataEntityLab);
        		Long labdataId = cdcDataEntityLab.getId();
        		if(CommonUtil.isNotEmpty(updatetimestr)) {
            		Date updatetime = sdf.parse(updatetimestr);
            		labitem.setUpdateDate(updatetime);
            	}
            	//获取更新人
            	if(CommonUtil.isNotEmpty(updateuserid)) {
            		CdcUserEntity cdcUserEntity = new CdcUserEntity();
            		cdcUserEntity.setXcode(updateuserid);
            		cdcUserEntity.setDataId(basedataId);
            		cdcUserEntity.setIsActive(1);
            		cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
            		//判断人员存在不存在
            		Long updateuser = null;
            		if(CommonUtil.isEmpty(cdcUserEntity)) {//不存在  保存到用户表里面
            			cdcUserEntity = new CdcUserEntity();
            			cdcUserEntity.setXcode(updateuserid);
                		cdcUserEntity.setIsActive(1);
            			cdcUserEntity.setCode("newuser"+updateuserid);
            			cdcUserEntity.setName("newuser"+updateuserid);
                		cdcUserEntity.setDataId(basedataId);
                		//保存用户
                		cdcUserMapper.insert(cdcUserEntity);
                		updateuser = cdcUserEntity.getId();
            		}else {
            			updateuser = cdcUserEntity.getId();
            		}
            		labitem.setUpdateBy(updateuser);
            	}
                if("1".equals(opercode)) {//保存/修改
                	if(CommonUtil.isNotEmpty(code)) {
                		labitem.setCode(code);
                	}
                	if(CommonUtil.isNotEmpty(description)) {
                		labitem.setDescription(description);
                	}
                	if(CommonUtil.isNotEmpty(significance)) {
                		labitem.setSignificance(significance);
                	}
                	if(CommonUtil.isNotEmpty(normalvalue)) {
                		labitem.setNormalValue(normalvalue);
                	}
                	if(CommonUtil.isNotEmpty(abbreviation)) {
                		labitem.setAbbreviation(abbreviation);
                	}
                	if(CommonUtil.isNotEmpty(note)) {
                		labitem.setNote(note);
                	}
                	//获取labvaluetypeId
                	if(CommonUtil.isNotEmpty(buslabvaluetypecode)) {
                		CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                    	cdcDictypeEntity.setCode("bus_lab_value_type");
                    	cdcDictypeEntity.setIsActive(1);
                    	cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                		Long diclabvaluetypeId = cdcDictypeEntity.getId();
                		//如果没有对应ID就要插入
                		CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                		cdcDictionaryEntity.setDictypeId(diclabvaluetypeId);
                		cdcDictionaryEntity.setCode(buslabvaluetypecode);
                		cdcDictionaryEntity.setIsActive(1);
                		cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                		//判断字典存在不存在
                		Long buslabvaluetypeid = null;
                		if(CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                			cdcDictionaryEntity = new CdcDictionaryEntity();
                			cdcDictionaryEntity.setDictypeId(diclabvaluetypeId);
                			cdcDictionaryEntity.setCode(buslabvaluetypecode);
                			cdcDictionaryEntity.setDescription(buslabvaluetypedesc);
                			cdcDictionaryEntity.setIsActive(1);
                    		//保存字典
                			cdcDictionaryMapper.insert(cdcDictionaryEntity);
                			buslabvaluetypeid = cdcDictionaryEntity.getId();
                		}else {
                			buslabvaluetypeid = cdcDictionaryEntity.getId();
                		}
                		labitem.setBusLabValueTypeId(buslabvaluetypeid);
                	}
                	labitem.setDataId(labdataId);
                	labitem.setIsActive(1);
                }else {
                	labitem.setIsActive(0);
                }
                //先判断是不是有该对象
                Wrapper<CdcLabItemEntity> wrapper =  new EntityWrapper<CdcLabItemEntity>();
                wrapper.eq("xcode", labitem.getXcode());
                wrapper.eq("data_id", labdataId);
                int count = this.selectCount(wrapper);
                if(count>0) {
                	this.update(labitem, wrapper);
                }else {
                	this.insert(labitem);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        } catch (Exception e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        }
		return "<response><status>1</status><message>ok</message></response>";
	}

}